package org.summerframework.core.util;

import org.summerframework.core.base.result.DefaultResultCode;
import org.summerframework.core.exception.WebException;
import org.apache.commons.codec.binary.Hex;
import org.springframework.util.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

/**
 * 阿里巴巴那边的子账号免登录token生成的md5
 */
public class Md5Util {

    private static final String DEFAULT_CHARSET = "UTF-8";
    private static final String[] hexDigits = {"0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    public final static String createMD5(byte[] strTemp) {

        return DigestUtils.md5DigestAsHex(strTemp);

        //String resultString = null;
        //try {
        //    MessageDigest md = MessageDigest.getInstance("MD5");
        //    resultString = byteArrayToHexString(md.digest(strTemp));
        //} catch (Exception exception) {
        //}
        //return resultString;
    }

    public final static String createMD5(File file) {
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            MessageDigest MD5 = MessageDigest.getInstance("MD5");

            byte[] buffer = new byte[8192];
            int length;

            while ((length = fileInputStream.read(buffer)) != -1) {
                MD5.update(buffer, 0, length);
            }

            return new String(Hex.encodeHex(MD5.digest()));
        } catch (Exception e) {
            throw new WebException(DefaultResultCode.SYSTEM_ERROR);
        }
    }

    public final static String createMD5(String source) {
        try {
            return createMD5(source.getBytes(DEFAULT_CHARSET));
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    /**
     * MD5加密
     *
     * @param b
     * @return
     */
    private static String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }

        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n += 256;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname)) {
                resultString = byteArrayToHexString(md.digest(origin
                        .getBytes()));
            } else {
                resultString = byteArrayToHexString(md.digest(origin
                        .getBytes(charsetname)));
            }
        } catch (Exception exception) {
        }
        return resultString;
    }

}
